package com.axones.planningpoker.rest.service;

import org.restlet.data.Status;
import org.restlet.resource.Get;
import org.restlet.resource.ServerResource;

import com.axones.planningpoker.bean.User;
import com.axones.planningpoker.dao.UserDAO;
import com.google.gson.Gson;

public class AuthenticationResource extends ServerResource {
	private static UserDAO userDao;
	private static Gson gson;

	public AuthenticationResource() {
		if (userDao == null) {
			userDao = new UserDAO();
		}
		if (gson == null) {
			gson = new Gson();
		}
	}

	@Get
	public String doGet() {
		String email = (String) getRequest().getAttributes().get("email");
		String password = (String) getRequest().getAttributes().get("password");

		User user = userDao.find(email, password);
		
		if(user == null){
			setStatus(Status.CLIENT_ERROR_NOT_FOUND);
			return "";
		}
		
		setStatus(Status.SUCCESS_ACCEPTED);
		
		return gson.toJson(user);
	}
}
